//动态添加症状的js文件-------------------------------------------
var content2;
var boll2=true;
var boll=true;
var sysmpName_update;
var id_update;
$("#form_submit").on("click",function(){
    var formData = new FormData();
    var name = $("#name").val();
    var instruction = $("#instruction").val();
    var types = $("#types input[name='ispre']:checked").val();
    if ( name=="" || name.length>195 || instruction.length >195 ){
        if (  name.length>195 || instruction.length >195 ){
            if (name.length>195) {
                var add_massgae=document.getElementById("add_massgae");
                add_massgae.innerHTML="名称不能超过255";
                add_massgae.style.fontSize=1+"px";
                add_massgae.style.color="red";
                add_massgae.style.visibility="visible";
            }
            if ( instruction.length >195 ){
                var add_massgae=document.getElementById("add_massgae");
                add_massgae.innerHTML="说明不能超过255";
                add_massgae.style.fontSize=1+"px";
                add_massgae.style.color="red";
                add_massgae.style.visibility="visible";
            }
        } else {
            var add_massgae=document.getElementById("add_massgae");
            add_massgae.innerHTML="说明不能超过255";
            add_massgae.style.fontSize=1+"px";
            add_massgae.style.color="red";
            add_massgae.style.visibility="visible";
        }
        return false;
    }else {

        if (checkName()){
            checkName();
            formData.append("sysmpName",name);
            formData.append("sysmpInstruct",instruction);
            formData.append("reserve1",types);
            var  url="/SysmpType/addSysmptom";
            $.ajax(
                {
                    url: url,
                    type: 'POST',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false
                }
            ).done(
                function (res) {
                    window.location.href="/SysmpType/FindSysmp";
                    //location.reload(true);
                    $("#name").val("");
                    $("#instruction").val("");
                }
            ).fail(function (res) {
                alert("你的数据异常");
                return false;
            });
        }else {
            var add_massgae=document.getElementById("add_massgae");
            add_massgae.innerHTML="名称已存在";
            add_massgae.style.fontSize=1+"px";
            add_massgae.style.color="red";
            add_massgae.style.visibility="visible";
            return false;
        }
    }
});
//--------------------------------点击编辑按钮----------------------------
function update(id) {
    $.ajax({
        type: "GET",
        url: "/SysmpType/findOnl?systypeid="+id,
        dataType: "json",
        success: function (data) {
            id_update=data.emp.sysmpTypeId;
            sysmpName_update=data.emp.sysmpName;
            $("#name2").val(data.emp.sysmpName);
            $("#instruction2").val(data.emp.sysmpInstruct);
            if (data.emp.reserve1=="1"){
                $("#text").attr("checked",true);
            }
            if (data.emp.reserve1=="2"){
                $("#text_on").attr("checked",true);
            }
            if (data.emp.reserve1=="3"){
                $("#text_shang").attr("checked",true);
            }
        },error: function () {
            alert("发送请求失败！");
        }
    });
};
//-----------------------------------------提交更新开始---------------------------
$("#form_submit2").on("click",function () {
    if (confirm("您确定更新当前数据？")){
        if ( $("#name2").val().length=="" || $("#name2").val().length>195 || $("#instruction2").val().length>195 ){
            if (  $("#name2").val().length>195 || $("#instruction2").val().length>195 ){
                if ($("#name2").val().length>195) {
                    var updat_massgae=document.getElementById("updat_massgae");
                    updat_massgae.innerHTML="名称不能超过255";
                    updat_massgae.style.fontSize=1+"px";
                    updat_massgae.style.color="red";
                    updat_massgae.style.visibility="visible";
                }
                if ( $("#instruction2").val().length >195 ){
                    var updat_massgae=document.getElementById("updat_massgae");
                    updat_massgae.innerHTML="说明不能超过255";
                    updat_massgae.style.fontSize=1+"px";
                    updat_massgae.style.color="red";
                    updat_massgae.style.visibility="visible";
                }
            } else {
                var updat_massgae=document.getElementById("updat_massgae");
                updat_massgae.innerHTML="请输入完整信息";
                updat_massgae.style.fontSize=1+"px";
                updat_massgae.style.color="red";
                updat_massgae.style.visibility="visible";
            }
            return false;
        }else {
            if (checkName()){
                var formData2 = new FormData();
                formData2.append("sysmpTypeId",id_update);
                formData2.append("sysmpName",$("#name2").val());
                formData2.append("sysmpInstruct",$("#instruction2").val());
                formData2.append("reserve1",$("#types2 input[name='ispre']:checked").val());
                $.ajax(
                    {
                        type: 'POST',
                        cache: false,
                        url :"/SysmpType/updateSysmptom",
                        data: formData2,
                        processData: false,
                        contentType: false
                    }
                ).done(
                    function () {
                       // window.location.href="SysmpType/FindSysmp"
                        location.reload(true);
                    }
                ).fail(function () {
                    alert("你的数据异常");
                    return false;
                });
            }else {
                boll2=false;
            }
        }
        return boll2;
    }else {
        return false;
    }
});
///------------------------------------------提交更新结束-----------------------
function remov(id) {
    if (confirm("您确定删除当前穴位的数据？")) {
        $.ajax({
            type: "GET",
            url: "/SysmpType/deletSysmpType?sysmpTypeId=" + id,
            dataType: "json"
        }).done(
            function (res) {

            }).fail(
            function (res) {
                location.reload(true);
            });
    }
}
//-------------------------校验症状名称是否可以使用-----------------------
function updata_checkName() {
    var sysmpName=$("#name2").val();
    if (sysmpName_update==sysmpName) {
    }else {
        if (sysmpName==""){
            var updat_name=document.getElementById("updat_name");
            updat_name.innerHTML="名称不能为空";
            updat_name.style.fontSize=1+"px";
            updat_name.style.color="red";
            updat_name.style.visibility="visible";
        }else {
            $.ajax({
                type: "GET",
                url: "/SysmpType/checkName?sysmpName=" + sysmpName,
                dataType: "json"
            }).done(
                function (res) {
                    if (res.emp=="true"){
                        var updat_name=document.getElementById("updat_name");
                        updat_name.setAttribute("style","display:none");
                        boll=true;
                    }else {
                        var updat_name=document.getElementById("updat_name");
                        updat_name.innerHTML="名称已存在";
                        updat_name.style.fontSize=1+"px";
                        updat_name.style.color="red";
                        updat_name.style.visibility="visible";
                        boll=false;
                    }
                });
        }
    }
    return boll;
}
//-------------------------校验症状名称是否可以使用（添加时）-----------------------
function checkName() {
    var sysmpName=$("#name").val();
    if (sysmpName==""){
        var add_name=document.getElementById("add_name");
        add_name.innerHTML="名称不能为空";
        add_name.style.fontSize=1+"px";
        add_name.style.color="red";
        add_name.style.visibility="visible";
    }
    $.ajax({
        type: "GET",
        url: "/SysmpType/checkName?sysmpName=" + sysmpName,
        dataType: "json"
    }).done(
        function (res) {
            if (res.emp=="true"){
                boll=true;
            }else {
                var add_name=document.getElementById("add_name");
                add_name.innerHTML="名称已存在";
                add_name.style.fontSize=1+"px";
                add_name.style.color="red";
                add_name.style.visibility="visible";
                boll=false;
            }
        });
    return boll;
}
//禁用和启用------------------------------------------------
function start_id(sysmpTypeId,state){
    var formData = new FormData();
    formData.append("sysmpTypeId", sysmpTypeId);
    formData.append("state",state);
    if (state==1){
        $.ajax({
            type: "POST",
            cache: false,
            url: "/SysmpType/stateSysmptom",
            data: formData,
            processData: false,
            contentType: false
        }).done(
            function (res) {
                location.reload(true);
            }
        ).error(function () {
            alert("禁用失败");
        });
    } else {
        $.ajax({
            type: "POST",
            cache: false,
            url: "/SysmpType/stateSysmptom",
            data: formData,
            processData: false,
            contentType: false
        }).done(
            function () {
                location.reload(true);
            }
        ).error(function () {
            alert("启用失败");
        });
    }
}
//模糊查询----------------------------
$("#searchBtn").on("click",function () {
    search();
});
//回车事件--------------------------------------------
$("#searchContent").bind('keypress',function (event) {
    if (event.keyCode=="13"){
        search();
    }
});
//搜搜------------------------------------
function search() {
    content2 = $("#searchContent").val();
    $.cookie("content",content2,1);
    if (content2 === null || content2 === undefined || content2 === '' || content2.length === 0){
        alert("查询不能为空！");
    }else {
        window.location.href="/SysmpType/searchSysmptomType?content="+content2;
        $.ajax({
            type: 'POST',
            data: {"content": content2},
            url: "/SysmpType/searchSysmptomType",
            cache: false,
            success: function (data) {
                $("table").remove();
                $(".content_pageinfor_ul2").remove();
                $(".content_pageinfor_ul").remove();
                var $data = $(data); // 将整个文档转化为jquery对象
                var content = $data.find("table");
                var content_pageinfor=$data.find(".content_pageinfor_ul2");
                $(".content_pageinfor2").append(content_pageinfor);
                $(".tableDiv").append(content);
                $("#searchContent").val(content2);
            },
            error: function () {
                alert("请求失败！");
            }
        });
    }
}